<?php


namespace Sys\Controller;
use Common\Controller\BaseController;

class PercentController extends BaseController{
    public function show(){
        $res=M('exam_items')->where(array('id'=>array('in',array('3','4','5','6','7','14','17'))))->select();
        $add=0;
        foreach($res as $k=>$v){
            if($per=M('percent')->where(array('dict_id'=>$v['id']))->find()){
                $perc=$per['percent'];
            }else{
                $perc=0;
            }
            $add=$add+$perc;
            $res[$k]['percent']=$perc;
        }
		//dump($res);
        $this->assign('nu',$add);
        $this->assign('res',$res);
        $this->display();
    }
    public function add(){
        $id=explode(',',I('post.id'));
        $per=explode(',',I('post.per'));
		//dump($per);
		if($sum=array_sum($per)!=100){
			$res='修改失败,总和不等于100';
			$this->ajaxReturn($res);exit;
		}
	   foreach($id as $k=>$v){
            if(M('percent')->where(array('dict_id'=>$v))->find()){
				
                $data=[
                    'percent'=>$per[$k],
                ];
				if(!M('percent')->where(array('dict_id'=>$v,'percent'=>$per[$k]))->find()){
					if(M('percent')->where(array('dict_id'=>$v))->save($data)){
						$res='保存成功';
					}else{
						//$res='保存失败';
						$this->ajaxReturn($res);exit;
					}
				}
            }else{
                $data=[
                    'dict_id'=>$v,
                    'percent'=>$per[$k],
                ];
                if(M('percent')->add($data)){
                    $res='保存成功';
                }else{
                    //$res='保存失败';
                    $this->ajaxReturn($res);exit;
                }
            }
        }
        $this->ajaxReturn($res);
    }
}